package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class PromotionAction extends BaseAction<Promotion> {

	public static boolean CREATE_HTML = false;

	private final Logger Log = LoggerFactory.getLogger(PromotionAction.class);

	@Action(value = "promotion_pageQuery", results = {
			@Result(name = "success", type = "json") })
	public String pageQuery() {
		// 来自bos-fore的请求,要分页查询宣传内容并返回到前台页面上,需要使用WebService技术
		// 向后端系统bos-management的PromotionService接口中发送webService请求
		PageBean<Promotion> pageBean = WebClient
				.create("http://localhost:8080" + "/bos_management"
						+ "/services/promotionService/pageQuery?page=" + page
						+ "&rows=" + rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		// 将pageBean压入栈顶
		ActionContext.getContext().getValueStack().push(pageBean);
		return SUCCESS;
	}

	@Action(value = "promotion_showDetail")
	public String showDetail() throws Exception {
		// 先判断 id 对应html 是否存在，如果存在 直接返回
		String htmlRealPath = ServletActionContext.getServletContext()
				.getRealPath("/freemarker");
		File htmlFile = new File(htmlRealPath + "/" + model.getId() + ".html");

		Log.info("宣传活动详情页htmlFile=" + htmlFile);

		// 如果html文件不存在 ，查询数据库，结合freemarker模板生成 页面
		if (CREATE_HTML || !htmlFile.exists()) {
			Configuration configuration = new Configuration(
					Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(
					new File(ServletActionContext.getServletContext()
							.getRealPath("/WEB-INF/freemarker_templates")));
			// 获取模板对象
			Template template = configuration
					.getTemplate("promotion_detail.ftl");

			// 动态数据
			Promotion promotion = WebClient
					.create("http://localhost:8080/bos_management"
							+ "/services/promotionService/promotion/"
							+ model.getId())
					.accept(MediaType.APPLICATION_JSON).get(Promotion.class); // /bos_management
			Map<String, Object> parameterMap = new HashMap<String, Object>();
			parameterMap.put("promotion", promotion);

			// 合并输出
			template.process(parameterMap,
					new OutputStreamWriter(new FileOutputStream(htmlFile)));

		}

		CREATE_HTML = false;

		// 存在 ，直接将文件返回
		ServletActionContext.getResponse()
				.setContentType("text/html;charset=utf-8");
		// 读文件，写网络
		FileUtils.copyFile(htmlFile,
				ServletActionContext.getResponse().getOutputStream());

		// ServletActionContext.getResponse().getWriter().println("<h1>写给浏览器的</h1>");

		return NONE;
	}

}
